<?php
namespace app\model;
use think\model;

class User extends Model{

    protected $table = "user_login";
    // 查询单个用户的方法
//    public function queryUserById($id=6){
//        $user = User::find($id);
//        return $user->toArray();
//    }
//
//    // 获取器
//    public function getStatusAttr($value)
//    {
//        $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
//        return $status[$value];
//    }


    // 检查用户名是否存在
    public function checkUser($uname){
        $user = User::where('uname',$uname)->find();
        return $user;
    }

    // 检查密码是否正确
    public function checkUserPwd($uname,$upwd){
        $upwd= md5($upwd);
        $user = User::where('uname',$uname)->where('upwd',$upwd)->find();
        return $user;
    }

    //  添加用户方法
    public function insertUser($uname,$upwd){
       $user = User::create([
           'uname'=>$uname,
           // md5加密
           'upwd'=>md5($upwd),
       ]);
       return $user->id;
    }

    public function queryUserLike(){
        $user = User::withSearch(['name','age'],[
            'name'=>'J',
            'age'=>'16',
        ])->select();
        return $user->toArray();
    }

    // 自定义搜索器 search`FieldName`Attr,每个字段名对应一个搜索器
    public function searchNameAttr($query, $value, $data){
        $query->where('name','like', '%'.$value . '%');
    }

    public function searchAgeAttr($query, $value, $data){
        $query->where('age','>', $value );
    }
}